#include "UserListXml.h"

UserListXml::UserListXml()
{
    m_userList.clear();
}

QList<UserStruct> UserListXml::userList() const
{
    return m_userList;
}

bool UserListXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();


    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "User")    //解析账号
        {
            UserStruct user;
            QDomNode userChild = rootChild.toElement().firstChild();
            while(!userChild.isNull())
            {
                if(userChild.toElement().tagName() == "Level")
                {
                    QString level = userChild.toElement().text();
                    if(level == "Father")
                    {
                        user.setIsPrimary(true);
                    }
                    else
                    {
                        user.setIsPrimary(false);
                    }
                }
                else if(userChild.toElement().tagName() == "Name")
                {
                    user.setName(userChild.toElement().text());
                }
                else if(userChild.toElement().tagName() == "Account")
                {
                    user.setAccount(userChild.toElement().text());
                }
                else if(userChild.toElement().tagName() == "Password")
                {
                    user.setPassword(userChild.toElement().text());
                }
                else if(userChild.toElement().tagName() == "Role")
                {
                    user.setRole(userChild.toElement().text());
                }
                else if(userChild.toElement().tagName() == "Permission")
                {
                    user.setPermissionList(userChild.toElement().text().split(","));
                }
                userChild = userChild.nextSibling();
            }
            m_userList.append(user);
        }

        rootChild = rootChild.nextSibling();
    }

    return true;
}

bool UserListXml::modify(const QList<UserStruct> &userList)
{
    if(!this->clear())
    {
        return false;
    }

    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root=doc.documentElement();

    for(int i=0 ;i<userList.size(); i++)
    {
        UserStruct user = userList.at(i);
        m_userList.append(user);

        QDomText domText;
        QString  strTest;

        if(user.isPrimary())
        {
            strTest = "Father";
        }
        else
        {
            strTest = "Child";
        }
        QDomElement levelElement = doc.createElement("Level");
        domText = doc.createTextNode(strTest);
        levelElement.appendChild(domText);

        domText.clear();
        QDomElement nameElement = doc.createElement("Name");
        strTest = user.name();
        domText = doc.createTextNode(strTest);
        nameElement.appendChild(domText);

        domText.clear();
        QDomElement accountElement = doc.createElement("Account");
        strTest = user.account();
        domText = doc.createTextNode(strTest);
        accountElement.appendChild(domText);

        domText.clear();
        QDomElement passwordElement = doc.createElement("Password");
        strTest = user.password();
        domText = doc.createTextNode(strTest);
        passwordElement.appendChild(domText);

        domText.clear();
        QDomElement roleElement = doc.createElement("Role");
        strTest = user.role();
        domText = doc.createTextNode(strTest);
        roleElement.appendChild(domText);

        domText.clear();
        QDomElement permissionElement = doc.createElement("Permission");
        strTest = user.permissionList().join(",");
        domText = doc.createTextNode(strTest);
        permissionElement.appendChild(domText);

        QDomElement userElement = doc.createElement("User");
        userElement.appendChild(levelElement);
        userElement.appendChild(nameElement);
        userElement.appendChild(accountElement);
        userElement.appendChild(passwordElement);
        userElement.appendChild(roleElement);
        userElement.appendChild(permissionElement);

        root.appendChild(userElement);
    }

    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    return true;

}

bool UserListXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_userList.clear();
    return true;
}

